/**
 * @description 数据脱敏工具类
 *
 * Copyright (C) [2025] [版权所有者（北京资源律动科技有限公司）]. All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * 注意：本代码基于 AGPLv3 协议发布。若通过网络提供服务（如 Web 应用），
 * 必须公开修改后的完整源代码（包括衍生作品），详见协议全文。
 */
package com.kaifangqian.common.util;

import com.kaifangqian.utils.MyStringUtils;

/**
 * @author : zhh
 * create at:  2022/3/16
 */
public class DesensitizationUtils {
    /**
     * 手机号用****号隐藏中间数字
     *
     * @param phone
     * @return
     */
    public static String getPhone(String phone) {
        if (MyStringUtils.isNotBlank(phone)) {
            return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }
        return phone;
    }


    /**
     * 邮箱用****号隐藏前面的字母
     *
     * @return
     */
    public static String getEmail(String email) {
        if (MyStringUtils.isNotBlank(email)) {
            return email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4");
        }
        return email;
    }

    /**
     * 身份证号用****号隐藏中间数字
     *
     * @param idNumber
     * @return
     */
    public static String getIdNumber(String idNumber) {
        if (MyStringUtils.isNotBlank(idNumber)) {
            return idNumber.replaceAll("(\\d{6})\\d+(\\w{4})", "$1*****$2");
        }
        return idNumber;
    }

    /**
     *  脱敏
     * @param str 待脱敏字符串
     * @param left 左边保留多少位
     * @param right 右边保留多少位
     * @return 脱敏结果，除左右外，其余字符将被替换为*
     */
    public static String around(String str, int left, int right){
        String result = "*";
        if (str == null || (str.length() < left + right +1)){
            return str;
        }
        String regex = String.format("(?<=\\w{%d})\\w(?=\\w{%d})", left, right);
        return str.replaceAll(regex, result);
    }
}